package org.rkuk.kasutil.uintTest.framework 
{
	import flash.utils.getTimer;
	/**
	 * ...
	 * @author rkuk
	 */
	public class TestEnviorenment 
	{
		private static var singleton:TestEnviorenment;
		
		public static function get instance():TestEnviorenment
		{
			return singleton;
		}
		
		private var testResult_:Array;
		private var startTime_:int;
		
		public function TestEnviorenment() 
		{
			singleton = this;
			testResult_ = new Array();
		}
		
		public function startTest(classToTest:Class,methodName:String):void
		{
			testResult_.push(new TestResult(classToTest, methodName));
			startTime_ = getTimer();
		}
		
		public function runTest(target:Object, method:String, params:Array):Boolean
		{
			var stepTimeStart:int = getTimer();
			try
			{
				target[method].apply(target, params);
			}
			catch (e:Error)
			{
				if (e is TestFailure)
					addFailure(e);
				else if (e is TestWarning)
					addWarning(e);
				else
					addError(e);
			}
			currentResult.addStepTime(method, getTimer() - stepTimeStart);
		}
		
		public function endTest(clazz:Class,methodName:String):void
		{
			currentResult.totalTime = getTimer() - startTime_;
		}
		
		public function addFailure(failure:Error):void
		{
			currentResult.addFailure(failure);
		}
		
		public function addError(error:Error):void
		{
			currentResult.addError(error);
		}
		
		public function addWarning(warning:Error):void
		{
			currentResult.addWarning(warning);
		}
		
		private function get currentResult():TestResult
		{
			return testResult_[testResult_.length - 1] as TestResult;
		}
	}
}